﻿
@{
    ViewBag.Title = "UserEditView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>个人信息修改</legend>
</fieldset>
<div>
    <form class="layui-form" id="EditUserFrom" style="margin-top:10px;">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">原始密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="UserPassWord" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="NewPassWord" placeholder="不修改密码就不要输入" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">新密码确认</label>
                <div class="layui-input-inline">
                    <input type="password" name="NewPassWordConfirm" placeholder="不修改密码就不要输入" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-inline">
                    <input type="tel" name="Tel" lay-verify="required|phone" class="layui-input">
                </div>
            </div>
        </div>
        <input type="tel" name="UserID" style="display:none;" value="@ViewBag.UserID" class="layui-input">
        <button type="submit" id="mysubmit" class="layui-btn" lay-submit lay-filter="formVerify" style="display:none;">立即提交</button>
    </form>
</div>
<div class="layui-form-item" style="margin-top:20px;">
    <div class="layui-input-block">
        <button type="button" class="layui-btn" id="BtnSave">保存</button>
        @*<button type="button" class="layui-btn layui-btn-normal" onclick="testfunc()">默认内容</button>*@
    </div>
</div>

<script type="text/javascript">
    layui.use('form', function () {
        var form = layui.form;
        var IsValidate = 0;
        var IsValidateSMS = 0;
        var EnableBtn = 0;
        var EnableBtnConfirm = 0;
        var SendMSG = 0;
        var applydata = {};
        //监听提交，加入验证
        form.on('submit(formVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });

        form.on('submit(formVerifysms)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidateSMS = 1;
            return false;
        });
        //各种基于事件的操作，下面会有进一步介绍

        $("#BtnSave").on("click", function () {
            if (EnableBtn == 1) return;
            $("#mysubmit").click();
            if (IsValidate == 1) {
                var obj = $("#EditUserFrom").serializeArray();
                var data = {};
                $.each(obj, function (i, v) {
                    data[v.name] = v.value;
                });
                if (data["NewPassWord"] != "" || data["NewPassWordConfirm"] != "") {
                    if (data["NewPassWord"] != data["NewPassWordConfirm"]) {
                        return alert("两次新密码不相同");
                    }
                }

                //alert(JSON.stringify(data));
                
                $.ajax({
                    type: "POST",
                    url: "/User/SaveUser",
                    async: false,
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify(data),
                    success: function (msg) {
                        if (msg.result == "OK") {
                            alert("修改成功");
                            parent.layui.admin.events.closeThisTabs();
                        } else {
                            alert(msg.result);
                        }
                    }
                });
            }
        });


        $("#SMSConfirm").on("click", function () {
            if (SendMSG != 1) return;
            if (EnableBtnConfirm == 1) return;
            $("#mysubmit2").click();
            if (IsValidateSMS == 1) {
                var temptext = "";
                temptext = $("input[name='smscode']").val();
                applydata["smscode"] = temptext;
                EnableBtnConfirm = 1;
                $("#SMSConfirm").addClass("layui-btn-disabled");
                $("#loadingicon").css("display","block");
                $.ajax({
                    type: "POST",
                    url: "/GoodsInfo/CardApplyConfirm",
                    async: true,
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify(applydata),
                    success: function (msg) {
                        if (msg.result == "OK") {
                            $("#loadingicon").css("display", "none");
                            alert(msg.errmsg);
                            if (msg.trxstatus != "0000") {
                                $("#SMSConfirm").removeClass("layui-btn-disabled");
                                EnableBtnConfirm = 0;
                            }
                            else {

                            }
                        } else {
                            alert("新增失败");
                        }
                    }
                });
            }
            else {
                IsValidateSMS = 0;
            }


        });



    });

</script>